M = dlmread('ParsedData2.csv');
step = .01;

M0 = M(M(:,end-5) == 1,:); % no email
M1 = M(M(:,end-4) == 1,:); % mens email
M2 = M(M(:,end-3) == 1,:); % womens email

M0 = M0(M0(:,end-1)==1,:);
M1 = M1(M1(:,end-1)==1,:);
M2 = M2(M2(:,end-1)==1,:);

k = 0.5;
b0 = ridge(M0(:,end),[M0(:,1:19) M0(:,21:end-2)],k);
b1 = ridge(M1(:,end),[M1(:,1:19) M1(:,21:end-2)],k);
b2 = ridge(M2(:,end),[M2(:,1:19) M2(:,21:end-2)],k);

M0est = zeros(size(M0,1),1);
for i=1:size(M0,1)
    M0vect = [M0(i,1:19) M0(i,21:end-2)]';
    M0est(i,1) = sum(M0vect .* b0);
end
diff0 = M0(:,end) - M0est(:);

M1est = zeros(size(M1,1),1);
for i=1:size(M1,1)
    M1vect = [M1(i,1:19) M1(i,21:end-2)]';
    M1est(i,1) = sum(M1vect .* b1);
end
diff1 = M1(:,end) - M1est(:);

M2est = zeros(size(M2,1),1);
for i=1:size(M2,1)
    M2vect = [M2(i,1:19) M2(i,21:end-2)]';
    M2est(i,1) = sum(M2vect .* b2);
end
diff2 = M2(:,end) - M2est(:);

M_ = M;

% p(visit|x,treatment)
M = dlmread('ParsedData.csv');
M0 = M(M(:,9) == 0,:); % no email
M1 = M(M(:,9) == 1,:); % mens email
M2 = M(M(:,9) == 2,:); % womens email

visitbeta0 = zeros(1,29);
visitbeta1 = zeros(1,29);
visitbeta2 = zeros(1,29);
visitbeta0_ = zeros(7,29);
visitbeta1_ = zeros(7,29);
visitbeta2_ = zeros(7,29);
customer = zeros(1,29);

M0_ = zeros(7,3000,size(M,2));
M0_(1,:,:) = M0(1:3000,:);
M0_(2,:,:) = M0(3001:6000,:);
M0_(3,:,:) = M0(6001:9000,:);
M0_(4,:,:) = M0(9001:12000,:);
M0_(5,:,:) = M0(12001:15000,:);
M0_(6,:,:) = M0(15001:18000,:);
M0_(7,:,:) = M0(18001:21000,:);

M1_ = zeros(7,3000,size(M,2));
M1_(1,:,:) = M1(1:3000,:);
M1_(2,:,:) = M1(3001:6000,:);
M1_(3,:,:) = M1(6001:9000,:);
M1_(4,:,:) = M1(9001:12000,:);
M1_(5,:,:) = M1(12001:15000,:);
M1_(6,:,:) = M1(15001:18000,:);
M1_(7,:,:) = M1(18001:21000,:);

M2_ = zeros(7,3000,size(M,2));
M2_(1,:,:) = M2(1:3000,:);
M2_(2,:,:) = M2(3001:6000,:);
M2_(3,:,:) = M2(6001:9000,:);
M2_(4,:,:) = M2(9001:12000,:);
M2_(5,:,:) = M2(12001:15000,:);
M2_(6,:,:) = M2(15001:18000,:);
M2_(7,:,:) = M2(18001:21000,:);

for q = 1:7
    M0test = M0_(q,:,:);
    M1test = M1_(q,:,:);
    M2test = M2_(q,:,:);
    
    M0train = [];
    M1train = [];
    M2train = [];
    for t = 1:7
        if(t ~= q)
            M0train = [M0train; squeeze(M0_(t,:,:))];
            M1train = [M1train; squeeze(M1_(t,:,:))];
            M2train = [M2train; squeeze(M2_(t,:,:))];
        end
    end
    
    for j=1:20
        for i=1:size(M0train,1)
            % recency
            customer(0+M0train(i,1)) = 1;
            % history_segment
            customer(12+M0train(i,2)) = 1;
            % mens, womens
            customer(20:21) = M0train(i,4:5);
            % zip_code
            customer(22+M0train(i,6)) = 1;
            % newbie
            customer(25) = M0train(i,7);
            % channel
            customer(26+M0train(i,8)) = 1;
            %intercept
            customer(29) = 1;
            p = sigmoid(visitbeta0.*customer);
            for k=1:size(visitbeta0,2)
                visitbeta0(k) = visitbeta0(k) + step*((M0(i,10) - p)*customer(k) - .1*visitbeta0(k));
            end
        end

        for i=1:size(M1train,1)
            % recency
            customer(0+M1train(i,1)) = 1;
            % history_segment
            customer(12+M1train(i,2)) = 1;
            % mens, womens
            customer(20:21) = M1train(i,4:5);
            % zip_code
            customer(22+M1train(i,6)) = 1;
            % newbie
            customer(25) = M1train(i,7);
            % channel
            customer(26+M1train(i,8)) = 1;
            %intercept
            customer(29) = 1;
            p = sigmoid(visitbeta1.*customer);
            for k=1:size(visitbeta1,2)
                visitbeta1(k) = visitbeta1(k) + step*((M1(i,10) - p)*customer(k) - .1*visitbeta1(k));
            end
        end

        for i=1:size(M2train,1)
            % recency
            customer(0+M2train(i,1)) = 1;
            % history_segment
            customer(12+M2train(i,2)) = 1;
            % mens, womens
            customer(20:21) = M2train(i,4:5);
            % zip_code
            customer(22+M2train(i,6)) = 1;
            % newbie
            customer(25) = M2train(i,7);
            % channel
            customer(26+M2train(i,8)) = 1;
            %intercept
            customer(29) = 1;
            p = sigmoid(visitbeta2.*customer);
            for k=1:size(visitbeta2,2)
                visitbeta2(k) = visitbeta2(k) + step*((M2(i,10) - p)*customer(k) - .1*visitbeta2(k));
            end
        end    
    end
    
    visitbeta0_(q,:) = visitbeta0;
    visitbeta1_(q,:) = visitbeta1;
    visitbeta2_(q,:) = visitbeta2;
    q
end

% p(purchase|visit,x,treatment)
M0 = M(M(:,9) == 0,:); % no email
M1 = M(M(:,9) == 1,:); % mens email
M2 = M(M(:,9) == 2,:); % womens email

M0 = M0(M0(:,end-2)==1,:);
M1 = M1(M1(:,end-2)==1,:);
M2 = M2(M2(:,end-2)==1,:);

purchasebeta0 = zeros(1,29);
purchasebeta1 = zeros(1,29);
purchasebeta2 = zeros(1,29);
purchasebeta0_ = zeros(7,29);
purchasebeta1_ = zeros(7,29);
purchasebeta2_ = zeros(7,29);
customer = zeros(1,29);

M0_ = zeros(7,300,size(M,2));
M0_(1,:,:) = M0(1:300,:);
M0_(2,:,:) = M0(301:600,:);
M0_(3,:,:) = M0(601:900,:);
M0_(4,:,:) = M0(901:1200,:);
M0_(5,:,:) = M0(1201:1500,:);
M0_(6,:,:) = M0(1501:1800,:);
M0_(7,:,:) = M0(1801:2100,:);

M1_ = zeros(7,300,size(M,2));
M1_(1,:,:) = M1(1:300,:);
M1_(2,:,:) = M1(301:600,:);
M1_(3,:,:) = M1(601:900,:);
M1_(4,:,:) = M1(901:1200,:);
M1_(5,:,:) = M1(1201:1500,:);
M1_(6,:,:) = M1(1501:1800,:);
M1_(7,:,:) = M1(1801:2100,:);

M2_ = zeros(7,300,size(M,2));
M2_(1,:,:) = M2(1:300,:);
M2_(2,:,:) = M2(301:600,:);
M2_(3,:,:) = M2(601:900,:);
M2_(4,:,:) = M2(901:1200,:);
M2_(5,:,:) = M2(1201:1500,:);
M2_(6,:,:) = M2(1501:1800,:);
M2_(7,:,:) = M2(1801:2100,:);

for q=1:7
    M0test = M0_(q,:,:);
    M1test = M1_(q,:,:);
    M2test = M2_(q,:,:);
    
    M0train = [];
    M1train = [];
    M2train = [];
    for t = 1:7
        if(t ~= q)
            M0train = [M0train; squeeze(M0_(t,:,:))];
            M1train = [M1train; squeeze(M1_(t,:,:))];
            M2train = [M2train; squeeze(M2_(t,:,:))];
        end
    end
    
    for j=1:20
        for i=1:size(M0,1)
            % recency
            customer(0+M0(i,1)) = 1;
            % history_segment
            customer(12+M0(i,2)) = 1;
            % mens, womens
            customer(20:21) = M0(i,4:5);
            % zip_code
            customer(22+M0(i,6)) = 1;
            % newbie
            customer(25) = M0(i,7);
            % channel
            customer(26+M0(i,8)) = 1;
            %intercept
            customer(29) = 1;

            p = sigmoid(purchasebeta0.*customer);
            for k=1:size(purchasebeta0,2)
                purchasebeta0(k) = purchasebeta0(k) + step*((M0(i,end-1) - sigmoid(purchasebeta0.*customer))*customer(k) - .1*purchasebeta0(k));
            end
        end

        for i=1:size(M1,1)
            % recency
            customer(0+M1(i,1)) = 1;
            % history_segment
            customer(12+M1(i,2)) = 1;
            % mens, womens
            customer(20:21) = M1(i,4:5);
            % zip_code
            customer(22+M1(i,6)) = 1;
            % newbie
            customer(25) = M1(i,7);
            % channel
            customer(26+M1(i,8)) = 1;
            %intercept
            customer(29) = 1;

            p = sigmoid(purchasebeta1.*customer);
            for k=1:size(purchasebeta1,2)
                purchasebeta1(k) = purchasebeta1(k) + step*((M1(i,end-1) - sigmoid(purchasebeta1.*customer))*customer(k) - .1*purchasebeta1(k));
            end
        end

        for i=1:size(M2,1)
            % recency
            customer(0+M2(i,1)) = 1;
            % history_segment
            customer(12+M2(i,2)) = 1;
            % mens, womens
            customer(20:21) = M2(i,4:5);
            % zip_code
            customer(22+M2(i,6)) = 1;
            % newbie
            customer(25) = M2(i,7);
            % channel
            customer(26+M2(i,8)) = 1;
            %intercept
            customer(29) = 1;

            p = sigmoid(purchasebeta2.*customer);
            for k=1:size(purchasebeta2,2)
                purchasebeta2(k) = purchasebeta2(k) + step*((M2(i,end-1) - sigmoid(purchasebeta2.*customer))*customer(k) - .1*purchasebeta2(k));
            end
        end
    end
    
    purchasebeta0_(q,:) = purchasebeta0;
    purchasebeta1_(q,:) = purchasebeta1;
    purchasebeta2_(q,:) = purchasebeta2;
    q
end



final0 = zeros(1,size(M,1));
final1 = zeros(1,size(M,1));
final2 = zeros(1,size(M,1));
for n = 1:size(M,1)
    xcustomer = zeros(1,29);
    xcustomer(0+M(n,1)) = 1;
    % history_segment
    xcustomer(12+M(n,2)) = 1;
    % mens, womens
    xcustomer(20:21) = M(n,4:5);
    % zip_code
    xcustomer(22+M(n,6)) = 1;
    % newbie
    xcustomer(25) = M(n,7);
    % channel
    xcustomer(26+M(n,8)) = 1;    
    %intercept
    xcustomer(29) = 1;
    expcustomer = [M_(n,1:19) M_(n,21:end-2)]';
    
    
    probvisit0 = sigmoid(visitbeta0.*xcustomer);
    probvisit1 = sigmoid(visitbeta1.*xcustomer);
    probvisit2 = sigmoid(visitbeta2.*xcustomer);
    
    probpurch0 = sigmoid(purchasebeta0.*xcustomer);
    probpurch1 = sigmoid(purchasebeta1.*xcustomer);
    probpurch2 = sigmoid(purchasebeta2.*xcustomer);
    
    expected0 = sum(expcustomer.*b0);
    expected1 = sum(expcustomer.*b1);
    expected2 = sum(expcustomer.*b2);
    
    final0(n) = expected0*probpurch0*probvisit0;
    final1(n) = expected1*probpurch1*probvisit1;
    final2(n) = expected2*probpurch2*probvisit2;
end